{extend name="public/base"/}
{block name="style"}
{/block}
{block name="content"}
<div class="data-list" data-url="{:url('purchase')}">
    <form class="layui-form inline-form">
        <div class="pull-left">
            <div class="layui-inline">
                <input type="text" id="purchase-track" autofocus="autofocus" placeholder="输入采购单号/追踪号" class="layui-input input-order-sn">
            </div>
            <div class="layui-inline">
                <select name="producer_id" lay-filter="data-list" lay-search>
                    <option value="">供应商</option>
                    {foreach $producer as $p}
                    <option value="{$p.producer_id}">{$p.name}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" id="purchase-recv-search"><i class="layui-icon layui-icon-search"></i></button>
            </div>
        </div>
    </form>
    {if $is_show_batch}
    <div class="layui-row form-lists-button">
        <div class="layui-inline layui-btn-group">
            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="purchase-batch-recv"><i class="layui-icon layui-icon-cart"></i>批量收货</button>
        </div>
    </div>
    {/if}
    <div class="data">
        <p><i class="fa fa-spinner fa-spin"></i> 加载中...</p>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
layui.use(['layer', 'jquery', 'form', 'longli'], function()
{
    var layer = layui.layer
        , $ = layui.jquery
        , form = layui.form;

    // 收货
    $("#purchase-recv-search").click(function()
    {
        var $input = $("#purchase-track"),
            text = $.trim($input.val())
        if(!text) return false;
        var url = "{:url('purchase_recv')}?po=" + text;
        $.get(url).done(function(html)
        {
            if($.type(html) == "object")
            {
                if(html.data.type) layui.longli.playMP3(html.data.type);
                layer.msg(html.msg, {icon: 5, time: 1500});
                return false;
            }
            parent.layer.open({
                type: 1,
                title: "采购收货",
                content: html,
                scrollbar: false,
                area: ["80%", "80%"],
                btn: ['确定', '取消'],
                yes: function (index, layero)
                {
                    var $form = $("#purchase-recv");
                    $.post($form.attr('action'), $form.serialize(), function (response)
                    {
                        if(response.data.type) layui.longli.playMP3(response.data.type);
                        var icon = 5;
                        if(response.code == 1)
                        {
                            $(".data").closest('.data-list').getList();
                            icon = 6;
                            layer.close(index);
                        }
                        layer.msg(response.msg, {icon: icon, time: 1500});
                    });
                },
                success: function(layero, index){
                    form.render();
                },
                end: function()
                {
                    $input.focus();
                }
            });
        });
    });

    // 单个采购单收货
    $("body").on("click", ".purchase-recv", function()
    {
        var item = $(this).attr("item");
        if(!item) return false;
        var $input = $("#purchase-track");
        $input.val(item);
        $("#purchase-recv-search").click();
        $input.val('');
    });

    // 收货
    $("#purchase-track").keydown(function(event)
    {
        if(event.keyCode == 13)
        {
            $("#purchase-recv-search").click();
            $(this).val('');
        }
    });

    // 批量收货
    $("#purchase-batch-recv").click(function()
    {
        var ids = getIds('.purchase-id');
        if(ids.length < 1)
        {
            layer.msg("请选择要收货的采购单", {icon: 5, time: 1500});
            return false;
        }
        var url = "{:url('purchase_batch')}?ids=" + ids.join(",");
        layer.load(0, {shade: false});
        $.get(url).done(function(response)
        {
            layer.closeAll('loading');
            if(response.data.type) layui.longli.playMP3(response.data.type);
            var icon = 5;
            if(response.code == 1)
            {
                $(".data").closest('.data-list').getList();
                icon = 6;
            }
            layer.msg(response.msg, {icon: icon, time: 1500});
        });
    });
});
</script>
{/block}